export default {
	init:(Vue)=>{
		
		/**
		 * 判断是否登录
		 */
		// Vue.prototype.is_login = function() {
		// 	return Vue.prototype.is_login_fun();
		// }
		/**
		 * 判断是否登录
		 */
		Vue.prototype.is_login_fun = function() {
			let logined = uni.getStorageSync("logined");
			return logined == '1';
			// return typeof Vue.prototype.get_token() != "undefined" && Vue.prototype.get_token()!=""; 
		}
		
		Vue.prototype.refresh_wx_login_code = function(fun) {
			uni.login({
				provider: "toutiao",
				success(res) {
					uni.setStorageSync("wx_login_code", res.code);
					uni.setStorageSync("anonymous_code", res.anonymousCode);
					if (typeof fun == "function") {
						fun(res.code, res.anonymousCode);
					}
				}
			})
		}
		Vue.prototype.get_wx_login_code = function(fun_back) {
		
			uni.checkSession({
				success() {
					console.log("没有过期");
					if (typeof(fun_back) == "function") {
						fun_back(uni.getStorageSync("wx_login_code"), uni.getStorageSync("anonymous_code"));
					}
				},
				fail() {
		
					uni.login({
						provider: "toutiao",
						success(res) {
							console.log("过期了重新获取", res);
							uni.setStorageSync("wx_login_code", res.code);
							uni.setStorageSync("anonymous_code", res.anonymousCode)
							if (typeof(fun_back) == "function") {
								fun_back(res.code, res.anonymousCode);
							}
		
						}
					})
				}
			})
		
		}
		
		//在跳转之前，判断是否登录，
		/**
		 * @param {String} url 已登录则正常跳转
		 * @param {String} login_page_url 如未登录则跳转到该url 
		 */
		Vue.prototype.navto_by_check_login = function(url, login_page_url) {
			if (!Vue.prototype.is_login()) {
				uni.showModal({
					content: "需要登录才能进行操作,是否登录",
					confirmText: "去登录",
					confirmColor: "#07C160",
					success(res) {
						if (res.confirm) {
							if (login_page_url) {
								uni.reLaunch({
									url: login_page_url
								})
							} else {
								uni.reLaunch({
									url: "/pages/login/login"
								})
							}
						}
					}
				})
				return;
			} else {
				uni.navigateTo({
					url: url
				})
			}
		}
		/**
		 * 获取缓存的token
		 */
		Vue.prototype.get_token = function() {
			let token = uni.getStorageSync("token");
			return token;
		}
		/**
		 * 获取登录信息
		 */
		Vue.prototype.get_login_info = function() {
			let login_info = uni.getStorageSync("login_info");
			return login_info;
		}
		/**
		 * 清除本地登录信息
		 */
		Vue.prototype.clear_login_cache = function() {
			uni.removeStorageSync("logined");
			uni.removeStorageSync("login_info");
			uni.removeStorageSync("token");
		}
	}
}